﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Xml.Serialization;
using Fuse8.DomainFramework.BusinessService;
using Fuse8.DomainFramework.Domain;

namespace Fuse8.ContentManagementFramework.Domain
{
    [Serializable]
    public partial class CmsMediaContentFileLink : Int32EntityObject
    {
        #region Linked fields

        private CmsMediaContent _mediaContent;

        private CmsMediaFile _mediaFile;

        #endregion

        #region Properties

        public virtual int MediaContentId { get; set; }
        
        public virtual int MediaFileId { get; set; }

        public virtual bool IsPrimary { get; set; }

        #endregion

        #region Linked properties

        [XmlIgnore]
        [ScriptIgnore]
        public CmsMediaContent MediaContent
        {
            get
            {
                if (_mediaContent == null)
                {
                    var businessFacade = new BusinessServiceFacade<CmsMediaContent, int>();
                    if (businessFacade != null)
                    {
                        _mediaContent = businessFacade.FindOne(MediaContentId);
                    }
                }

                return _mediaContent;
            }
            set
            {
                MediaContentId = value != null ? value.Id : 0;

                if (_mediaContent != null)
                {
                    _mediaContent = _mediaContent.Id == MediaContentId ? value : null;
                }
                else
                {
                    _mediaContent = value;
                }
            }
        }

        [XmlIgnore]
        [ScriptIgnore]
        public CmsMediaFile MediaFile
        {
            get
            {
                if (_mediaFile == null)
                {
                    var businessFacade = new BusinessServiceFacade<CmsMediaFile, int>();
                    if (businessFacade != null)
                    {
                        _mediaFile = businessFacade.FindOne(MediaFileId);
                    }
                }

                return _mediaFile;
            }
            set
            {
                MediaFileId = value != null ? value.Id : 0;

                if (_mediaFile != null)
                {
                    _mediaFile = _mediaFile.Id == MediaFileId ? value : null;
                }
                else
                {
                    _mediaFile = value;
                }
            }
        }

        #endregion

        #region Constructor

        public CmsMediaContentFileLink()
        { }

        #endregion

        #region EntityObject override

        public override string TypeParameter
        {
            get
            {
                return "MediaContentFileLink";
            }
        }        

        #endregion
    }
}
